﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.Helper;
using CatUtils.Utils.SpecialUtils.Html;
using CatUtils.Utils.SpecialUtils.View;
using CatUtilsDll.Utils.Currency.WebUtils;
using RabbitDownload.Helper;
using RabbitDownload.Helper.Currency;

using RabbitDownload.Helper.ViewHelper;
using RabbitDownload.WebSite.Local.TagManger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RabbitDownload.WebSite.Zerochan
{
    internal class ZerochanTag
    {
        public const string ZerochanTagApi = "https://www.zerochan.net/tags?s=count&p={0}";
        public static string ZerochanTagFile = StaticValue.TempPath + "ZerochanTag.txt";

        /// <summary>
        /// 下载图像标签
        /// </summary>
        /// <param name="ImgCountMax"></param>
        public void DownloadTag(string ImgMaxCountText)
        {
            if (ImgMaxCountText == "")
                return;
            int ImgMaxCount = TextHelper.StringToInt(ImgMaxCountText);
            int PageCount = 1;
            while (true)
            {
                if (!SacnnerList(PageCount++, ImgMaxCount))
                    break;
            }
            MessageHelper.OpenFloderAlert(StaticValue.TempPath);
        }

        private bool SacnnerList(int PageCount, int ImgMaxCount)
        {
            string API = string.Format(ZerochanTagApi, PageCount);
            Console.WriteLine(API);
            string Shtml = WebHelper.HttpPcGet(API);
            bool JumpFlag = true;
            List<string> HtmlVlaueList = HtmlHelper.GetHtmlValueList(Shtml, "//*[@id=\"content\"]/ul/li");
            if (HtmlVlaueList.Count <= 0)
                JumpFlag = false;
            foreach (var item in HtmlVlaueList)
            {
                string TagText = HtmlHelper.GetFirstText(item, "//a");
                string ImgCountText = HtmlHelper.GetFirstText(item, "//li");
                if (TagText == "View")
                    continue;
                if (ImgCountText != "")
                    ImgCountText = ImgCountText.Replace(TagText, "").Trim();
                int ImgCount = TextHelper.StringToInt(ImgCountText);

                if (ImgCount < ImgMaxCount)
                {
                    JumpFlag = false;
                    continue;
                }

                FileHelper.AppendText(ZerochanTagFile, string.Format(LocalTagStatcivalue.TagInfoMode, TagText, ImgCount));
            }
            return JumpFlag;
        }
    }
}